define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/op/models'),
        user_models = require('js/apps/console/models'),
        utils = require('js/utils/tools'),
        tz = require('js/utils/tz'),
        cst = require('js/frame/const'),
        app = Backbone.history;

    require('datetimepicker');
    require('multiselect');
    require('magnific');

    var GameOrderListView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch'
        },
        initialize: function (options) {
            this.options = options;
            this.collection = new models.GameOrderCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },

        _getSearchOption: function () {
            var options = {};
            var searched = utils.getAllInput('#sidebar_right');
            searched.game = $('#searchActivityType').val();
            searched.user_id = parseInt($('#searchUserID').val());
            searched.created_at_lower = $("#searchCreatedAtLower").val();
            searched.created_at_upper = $('#searchCreatedAtUpper').val();
            if (searched.created_at_lower || searched.created_at_upper) {
                options.created_at = {};
                if (searched.created_at_lower) options.created_at.$gte = searched.created_at_lower;
                if (searched.created_at_upper) options.created_at.$lt = searched.created_at_upper;
            }
            if(searched.game != -1){
                options.game = searched.game;
            }
            if (!isNaN(searched.user_id)) {
                options.user_id = searched.user_id;
            }

            utils.saveSearched('op/game_order/', searched);
            return options;
        },

        doSearch: function () {
            var options = this._getSearchOption();
            options.$page = 1;
            options.$size = 30;
            options._ = new Date().getTime();
            app.navigate(utils.composeQueryString('#op/game_order/', options), {
                trigger: true
            });
        },

        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                bet_total: this.collection.bet_total,
                award_total: this.collection.award_total,
                searched: utils.getSearched('op/game_order/')
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#op/game_order/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('[data-type=date]').datetimepicker({
                defaultDate: false,
                format: 'YYYY-MM-DD HH:mm:ss'
            });

            $('.modify-item').magnificPopup({
                items: {
                    src: '#editWithdrawPanel',
                    type: 'inline'
                },
                midClick: true,
                closeOnBgClick: false,
                callbacks: {
                    beforeOpen: function () {
                        $('#riskInfo').html('');
                        var data_id = $(this.st.el).closest('tr').data('id'),
                            choosen = that.collection.get(data_id);
                        $('#order_id').text(choosen.get('id'));
                        $('#user_id').text(choosen.get('user_id'));
                        $('#activity_type').text(window.GAME_TYPE[choosen.get('type')]);
                        if(101 == choosen.get('type')){
                           $('#bet_name').text(window.BULL_INDEX[choosen.get('bet_index')]);
                           $('#term').text(choosen.get('term_number'));
                        }else if(103 == choosen.get('type')){
                           $('#bet_name').text(window.FRUIT_INDEX[choosen.get('bet_index')]);
                           $('#term').text(choosen.get('term_number'));
                        }else if(104 == choosen.get('type')){
                           $('#bet_name').text(window.LOTTERY_INDEX[choosen.get('bet_index')]);
                           $('#term').text(choosen.get('term_number'));
                        }
                        $('#price').text(choosen.get('bet_amount'));
                        $('#win_price').text(choosen.get('award_amount'));
                        $('#created_at').text(choosen.get('created_at'));
                        $('#updated_at').text(choosen.get('updated_at'));

                        $('#submitClose').off('click').click(function() {
                            $.magnificPopup.close();
                        });

                    },
                }
            });

            utils.getPaginator(that.options, this.collection.total, '#op/game_order/');

            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/GameOrders.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            this.options.$size = 20;
            if(null == this.options.game){
                this.options.game = 'lottery';
            }
            var view = new GameOrderListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });
});